import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    require: '/login',
    component: () => import('@/views/Login'),
    meta: { title: "登录" }
  }, {
    path: '/home',
    name: 'home',
    component: () => import('@/views/Layout/Home.vue'),
    meta: { title: '首页' },
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/Index'),
        meta: { title: '主页' },
      },
      {
        path: '/building',
        name: 'building',
        component: () => import('@/views/Building'),
        meta: { title: '楼宇管理' },
      },
      {
        path: '/update',
        name: 'update',
        component: () => import('@/views/Update'),
        meta: { title: '修改密码' },
      },
      {
        path: '/private',
        name: 'private',
        component: () => import('@/views/Private'),
        meta: { title: '个人资料' }
      },
      {
        path: '/admin',
        name: 'admin',
        component: () => import('@/views/Admin'),
        meta: { title: '超管管理' }
      },
      {
        path: '/dormitoryManager',
        name: 'dormitoryManager',
        component: () => import('@/views/DormitoryManager'),
        meta: { title: '宿管管理' }
      }, {
        path: '/dormitory',
        name: 'dormitory',
        component: () => import('@/views/Dormitory'),
        meta: { title: '宿舍管理' }
      },
      {
        path: '/student',
        name: 'student',
        component: () => import('@/views/Student'),
        meta: { title: '学生管理' }
      },
      {
        path: '/live',
        name: 'live',
        component: () => import('@/views/Live'),
        meta: { title: '入住登记' }
      }, {
        path: '/repair',
        name: 'repair',
        component: () => import('@/views/Repair'),
        meta: { title: '报修管理' }
      },
      {
        path: '/score',
        name: 'score',
        component: () => import('@/views/Score'),
        meta: { title: '宿管评分' }
      }
      
    ]
  },
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router


//路由守卫
router.beforeEach((to, from, next) => {
  let token = window.sessionStorage.getItem("token")//查看登录token
  if (to.path == '/') {
    if (token) {
      next({ path: '/home' })
    }
  } else if (token == null) {
    Vue.prototype.$message({
      type: "error",
      message: "请先登录"
    });
    next({ path: '/' })
  }
  next()
})
